package com.ren.stuyspringbootmore.controller;

import com.ren.stuyspringbootmore.Entity.Department;
import com.ren.stuyspringbootmore.Entity.Employee;
import com.ren.stuyspringbootmore.dao.DepartmentDao;
import com.ren.stuyspringbootmore.dao.EmployeeDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.jws.WebParam;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Map;

@Controller
public class EmployeeController {
    @Autowired
    EmployeeDao employeeDao;
    @Autowired
    DepartmentDao departmentDao;

    @GetMapping("/emps")
    public String list(Model model){
        Collection<Employee> all = employeeDao.getAll();
        model.addAttribute("emmps",all);
        return "/emp/list";
    }


    @GetMapping("/emp")
    public String toAddpage(Model model){
        Collection<Department> departments = departmentDao.getDepartments();

        model.addAttribute("depts",departments);
        return "/emp/add";
    }

    @PostMapping("/emp")
    //springmvc自动将请求参数和入参对象属性进行绑定；要求请求参数的名字和javabean入参的对象属性名字一样
    public String addEmp(Employee employee){
//        redirect
//        forwaord

//        System.out.println(employee);
        employeeDao.save(employee);
        return "redirect:/emps";
    }
    @GetMapping("/emp/{id}")
    public String toEditPage(@PathVariable("id") Integer id, Model model){
        Employee employee = employeeDao.get(id);
        model.addAttribute("emp",employee);

        Collection<Department> departments = departmentDao.getDepartments();
        model.addAttribute("depts",departments);
        return "emp/edit";
    }

}

